#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef struct node
{

	struct node* next;
	int data;


}node;
typedef struct queue
{
	node* prev;
	node* tail;



}queue;
void QueueInit(queue* q)
{
	q->prev = NULL;
	q->tail = NULL;

}
void QueuePush(queue* q, int x)
{    

	node* newnode = (node*)malloc(sizeof(node));
	if (newnode == NULL)
	{
		perror("malloc fail");
	}
	newnode->next = NULL;
	newnode->data = x;
	if (q->prev == NULL)
	{
		q->prev = q->tail =newnode;
	}
	else
	{
		q->tail->next = newnode;
		q->tail = newnode;


	}
}
void QueuePop(queue* q)
{
	assert(q->prev);
	if (q->prev->next == NULL)
	{
		q->prev = q->tail = NULL;

	}
	else
	{
		node* next = q->prev->next;
		free(q->prev);
		q->prev =q->tail= next;



	}


}
int QueueFront(queue* q)
{
	assert(q->prev);
	return q->prev->data;



}
int QueueBack(queue* q)
{

	assert(q->prev);
	return q->tail->data;

}
int QueueSize(queue* q)
{
	int x = 0;
	node* cur = q->prev;
	while (cur)
	{
		x++;
		cur = cur->next;
	}
	return x;


}
int queueEmpty(queue* q)
{
	return q->prev == NULL;

}
void QueueDestroy(queue* q)
{
	node* cur = q->prev;
	while (cur)
	{
		node* next = cur->next;
		free(cur);
		cur = next;
	}

}
int main()
{
	queue st;
	QueueInit(&st);
	QueuePush(&st, 1);
	QueuePush(&st, 2);
	QueuePush(&st, 3);
	QueuePush(&st, 4);
	while (!queueEmpty(&st))
	{
		printf("%d ", QueueFront(&st));
		QueuePop(&st);


	}



	QueueDestroy(&st);









}
